/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.angholt.bookproject.db.entity;

import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

/**
 * Entity class to store informashen about user intrested books
 * @author Patric Peterson
 */
@Entity(name="have_read")
public class Read implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long readId;
    
    @ManyToOne(cascade= {CascadeType.MERGE, CascadeType.REFRESH},fetch= FetchType.LAZY)
    @JoinColumn(nullable=false)
    private User user;
   
    @OneToOne
    @JoinColumn(nullable=false)
    private Book read;
    
    @Column(length=15)
    private String tabelName;

    /**
     * Construct an empty read element.
     * need to fill read, user.
     */
    public Read() {
    }

    /**
     * construct a full read element.
     * @param user The user that is conserning.
     * @param read The book that is intrested.
     * @param tabelName The name of the tabel this is in.
     */
    public Read(User user, Book read, String tabelName) {
        this.user = user;
        this.read = read;
        this.tabelName = tabelName;
    }
    
    /**
     * Get the entity id.
     * @return entity id.
     */
    public Long getReadId() {
        return readId;
    }
    
   /**
     * set entity id.
     * shout not be changed exept by jpa.
     * @param id entity id
     */
    public void setReadId(Long id) {
        this.readId = id;
    }

    /**
     * Get the read book.
     * @return read book.
     */
    public Book getRead() {
        return read;
    }

    /**
     * Set the read book.
     * @param read Read book.
     */
    public void setRead(Book read) {
        this.read = read;
    }

    /**
     * Get the name of the table.
     * @return Name of the table.
     */
    public String getTabelName() {
        return tabelName;
    }

    /**
     * Set the name of the table. ( less then 15 chars )
     * @param tabelName Name of the table.
     */
    public void setTabelName(String tabelName) {
        this.tabelName = tabelName;
    }

    /**
     * Get the user that is relevent.
     * @return User that is relevent.
     */
    public User getUser() {
        return user;
    }

    /**
     * Set the user that is relevent.
     * @param user User that is relevent.
     */
    public void setUser(User user) {
        this.user = user;
    }
   
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (readId != null ? readId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TO DO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Read)) {
            return false;
        }
        Read other = (Read) object;
        if ((this.readId == null && other.readId != null) || (this.readId != null && !this.readId.equals(other.readId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "edu.chl.angholt.bookproject.db.entity.Read[ id=" + readId + " ]";
    }
    
}
